package com.yanqu.road.server.logic.task.condition;

import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.mall.MallModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class MammonShopWakeupCondition extends BaseCondition {


    public MammonShopWakeupCondition(TaskConditionInfo taskConditionInfo, BaseTaskData baseTaskData, BigInteger value, Object initArgs) {
        super(taskConditionInfo, baseTaskData, value, initArgs);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.MammonShopWakeupProcess.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.MammonShopWakeupProcess.getValue(), handle);
    }

    @Override
    protected void changeValue(Object object) {
        MallInfo mallInfo = (MallInfo) object;
        if(mallInfo.getShopType() != eShopType.MammonMall.getValue()){
            return;
        }
        initValue();
    }

    @Override
    public void initValue() {
        //服装组合id
        int comboId = taskConditionInfo.getParamList().get(0).intValue();
        //服装列表
        List<Integer> skinIdList = SkinMgr.getSkinListByComb(comboId);
        List<MallInfo> mallInfoList = MallMgr.getMallInfoListByShopType(eShopType.MammonMall.getValue());
        //已购服装（从财神商店）
        List<Integer> buyedSkinIdList = new ArrayList<>();
        MallModule mallModule = player.getModule(MallModule.class);
        for (MallInfo mallInfo : mallInfoList) {
            int buyCount = mallModule.getMallBuyCount(mallInfo.getMallId());
            if(buyCount > 0){
                Property property = PropertyHelper.parseStringToProperty(mallInfo.getReward());
                if(property.getGoods().size() == 1){
                    List<Integer> item = StringUtils.stringToIntegerList(mallInfo.getReward(), "=");
                    if(!skinIdList.contains(item.get(0))){
                        continue;
                    }
                    if(buyCount >= taskConditionInfo.getParamList().get(2).intValue()) {
                        buyedSkinIdList.add(item.get(0));
                    }
                }
            }
        }
        setValue(BigInteger.valueOf(buyedSkinIdList.size()));
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(taskConditionInfo.getParamList().get(1)) >= 0;
    }
}
